Skip to content

fix: Correct sort length access in applyQueryOptions #40190

Open
Naetiksoni08 wants to merge 4 commits intoRocketChat:developfrom
Naetiksoni08:fix/apply-query-options-sort-length
Open

fix: Correct sort length access in applyQueryOptions #40190
Naetiksoni08 wants to merge 4 commits intoRocketChat:developfrom
Naetiksoni08:fix/apply-query-options-sort-length

Conversation

@Naetiksoni08
Copy link
Copy Markdown
Contributor

@Naetiksoni08 Naetiksoni08 commented Apr 17, 2026

Proposed changes (including videos or screenshots)

In applyQueryOptions.ts, the sort loop was incorrectly accessing sortObj.sort.length instead of sortObj.length.

sortObj is an array returned by convertSort(). Accessing .sort on an array returns the built-in Array.prototype.sort
method (a function), not the array's length. Since Array.prototype.sort accepts 1 argument, .sort.length always returns
1 regardless of how many sort fields exist.

Before:

for (let i = sortObj.sort.length - 1; i >= 0; i--) {

After:

for (let i = sortObj.length - 1; i >= 0; i--) {

Impact of the bug:

  • The loop always ran exactly once, applying only one sort field
  • Multi-field sorting (e.g. { lm: -1, name: 1 }) was silently broken — only the first sort field was ever applied
  • TypeScript didn't catch this because Array.prototype.sort is a valid property on arrays

This affects anywhere applyQueryOptions is called with multi-field sort options, including UserProvider and SettingsProvider.

Steps to test or reproduce

// Proof in browser console:
const arr = ['a', 'b', 'c'];
console.log(arr.sort.length); // always returns 1 which is wrong
console.log(arr.length); // returns 3

Further comments

No tests existed for applyQueryOptions so this was never caught. The fix is a one-word change removing the erroneous .sort access.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected sorting behavior so all specified sort fields are now consistently applied when running queries, ensuring search and list results honor the intended sort order.

@Naetiksoni08 Naetiksoni08 requested a review from a team as a code owner April 17, 2026 07:54
@dionisio-bot
Copy link
Copy Markdown
Contributor

dionisio-bot Bot commented Apr 17, 2026

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is targeting the wrong base branch. It should target 8.5.0, but it targets 8.4.0

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 17, 2026

⚠️ No Changeset found

Latest commit: 33c883d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 17, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 265532ad-df22-435a-a869-e7af3a44834b

📥 Commits

Reviewing files that changed from the base of the PR and between 043ec2e and eb7c8d5.

📒 Files selected for processing (1)
  • apps/meteor/client/lib/cachedStores/applyQueryOptions.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • apps/meteor/client/lib/cachedStores/applyQueryOptions.ts

Walkthrough

Updated iteration bounds in applyQueryOptions.ts: the reverse loop over converted sort definitions now uses sortObj.length instead of sortObj.sort.length, ensuring the loop iterates over the actual SortObject array.

Changes

Cohort / File(s) Summary
Loop Bounds Fix
apps/meteor/client/lib/cachedStores/applyQueryOptions.ts
Replaced sortObj.sort.length with sortObj.length in reverse iteration to fix incorrect array indexing when applying converted sort definitions.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Suggested labels

type: bug

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: correcting the sort length access from sortObj.sort.length to sortObj.length in applyQueryOptions, which is a one-word fix addressing a critical bug in multi-field sorting.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

@tassoevan tassoevan added this to the 8.4.0 milestone Apr 17, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 69.85%. Comparing base (987728a) to head (33c883d).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##           develop   #40190    +/-   ##
=========================================
  Coverage    69.85%   69.85%            
=========================================
  Files         3296     3291     -5     
  Lines       119166   119054   -112     
  Branches     21482    21411    -71     
=========================================
- Hits         83242    83171    -71     
+ Misses       32628    32594    -34     
+ Partials      3296     3289     -7     
Flag Coverage Δ
unit 70.59% <ø> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@tassoevan tassoevan added this pull request to the merge queue Apr 17, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 17, 2026
@Naetiksoni08
Copy link
Copy Markdown
Contributor Author

hey @tassoevan, merge queue removed the PR due to failed Test UI (EE) / MongoDB checks. These seem unrelated to my change (1 line fix in applyQueryOptions.ts). Could you help re-queue or confirm if these are flaky tests?

@Naetiksoni08 Naetiksoni08 requested a review from tassoevan April 17, 2026 16:28
@Naetiksoni08 Naetiksoni08 force-pushed the fix/apply-query-options-sort-length branch from 043ec2e to eb7c8d5 Compare April 19, 2026 12:19
@coderabbitai coderabbitai Bot removed the type: bug label Apr 19, 2026
@scuciatto scuciatto modified the milestones: 8.4.0, 8.5.0 Apr 20, 2026
@tassoevan tassoevan added the stat: QA assured Means it has been tested and approved by a company insider label Apr 20, 2026
@Naetiksoni08
Copy link
Copy Markdown
Contributor Author

Hi @tassoevan , the milestone has been updated to 8.5.0. Could you re-add this to the merge queue? Thank you!

@tassoevan tassoevan enabled auto-merge April 21, 2026 05:25
@Naetiksoni08
Copy link
Copy Markdown
Contributor Author

@tassoevan , Dionisio QA is still showing the milestone as 8.4.0. Could you check if the milestone on the PR is correctly set to 8.5.0? The check seems to be picking up the old value.

Screenshot 2026-04-21 at 1 10 43 PM

@Naetiksoni08 Naetiksoni08 requested a review from tassoevan April 21, 2026 11:28
@dougfabris
Copy link
Copy Markdown
Member

@Naetiksoni08 don't worry, its happening because we're in the middle of the release cut

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community stat: QA assured Means it has been tested and approved by a company insider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants